package com.ruoyi.web.core.config;

import com.ruoyi.web.util.ResponseCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.lang.NonNull;

import java.io.Serializable;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("Controller返回对象")
public class ResultDTO<T> implements Serializable {

    @ApiModelProperty("处理状态码")
    private String code;
    @ApiModelProperty("返回消息")
    private String message;
    @ApiModelProperty("返回数据")
    private T data;

    public ResultDTO(@NonNull ResponseCode responseCode) {
        this.code = responseCode.getCode();
        this.message = responseCode.getDes();
    }

    public static <T> ResultDTO<T> success() {
        return custom(ResponseCode.SUCCESS);
    }

    public static <T> ResultDTO<T> success(T data) {
        return custom(ResponseCode.SUCCESS, data);
    }

    public static <T> ResultDTO<T> error() {
        return custom(ResponseCode.SYSTEM_ERROR);
    }

    public static <T> ResultDTO<T> error(T data) {
        return custom(ResponseCode.SYSTEM_ERROR, data);
    }

    public static <T> ResultDTO<T> custom(ResponseCode responseCode) {
        return new ResultDTO<>(responseCode);
    }

    public static <T> ResultDTO<T> custom(ResponseCode responseCode, T data) {
        return new ResultDTO<>(responseCode.getCode(), responseCode.getDes(), data);
    }

    /**
     * 判断是否成功
     */
    public boolean isSuccess() {
        return ResponseCode.SUCCESS.getCode().equals(code);
    }

    /**
     * 自定义消息，返回码：100010
     */
    public static <T> ResultDTO<T> customMsg(String msg) {
        ResultDTO<T> result = new ResultDTO<T>();
        result.setCode(ResponseCode.REMIND_ERR.getCode());
        result.setMessage(msg);
        return result;
    }

    /**
     * 自定义消息，返回码：100010
     */
    public static <T> ResultDTO<T> customMsg(String msg, T data) {
        return new ResultDTO<T>(ResponseCode.REMIND_ERR.getCode(), msg, data);
    }

}
